
import requests

from conn_mysql.conn_mysql import ConnectMysql

class ApiIndex(ConnectMysql):

    def index(self, methods, page, limit, party):

        if not methods == 'GET':
        
            return {
                'status': -1,
                'errorCode': 4,
                'errorMsg':f'请检查request.method:{methods}请重新请求',
                'resultBody':None
            }
    
        if page == None:
        
            page = '1'

        if limit == None:

            limit = '20'

        if page.isdecimal() and limit.isdecimal(): #检查参数合法性

            page = int(page)

            limit = int(limit)

        else:

            return {
                'status': -1,
                'errorCode': 3,
                'errorMsg':f'请检查page和limit参数是否合法,page:{page},limit:{limit}',
                'resultBody':None
            }

        if party == None:

            sql = 'select id,company_name,linkman,need,party,update_time from company ORDER BY update_time DESC limit %s,%s '

            result_info = self.select_all(sql, ((page-1)*limit, limit))

            return {

                'status': 1,
                'errorCode': None,
                'errorMsg':None,
                'resultBody': result_info,
            }
        
        else:

            sql = "select id,company_name,linkman,need,party,update_time from company WHERE party=%s ORDER BY update_time DESC limit %s,%s "

            result_info = self.select_all(sql, (party, (page-1)*limit, limit))

            return {

                'status': 1,
                'errorCode': None,
                'errorMsg':None,
                'resultBody': result_info,
            }

        